package yxy.game.pm2.bean.db.newDao;

import yxy.apple.db.DbManager;
import yxy.game.pm2.bean.db.po.OrderInfoPo;
import yxy.game.pm2.bean.db.po.SoulPo;
import yxy.game.pm2.bean.order.OrderInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liangjianbin
 * @date 2022/3/1 8:40
 **/
public class OrderInfoDao {
    private static final String
            REPLACE_ONE = "replace into `orderInfo` (`pCode`, `goodsId`, `serverId`,`myOrderId`,`platOrderId`, `goodsPrice`," +
            " `payaMount`,`actType`, `orderState`,`channel`, `orderCreateTime`,`upDateTime`,`curs`,`addVipExp`,`saveToDbTime`) " +
            "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
            SELECT_ALL = "select * from `orderInfo` where `pCode` = ?",
            DELETE = "delete from `orderInfo` where `pCode` = ? and `saveToDbTime` != ?";

    public static List<OrderInfoPo> getList(String playerCode) {
        return DbManager.instance().executeQueryObjectList(SELECT_ALL, new Object[]{playerCode}, OrderInfoPo.builder);
    }

    public static boolean replace(List<OrderInfoPo> orderInfoPoList) {
        List<Object[]> list = new ArrayList<>();
        for (OrderInfoPo orderInfoPo : orderInfoPoList) {
            Object[] objects = new Object[]{orderInfoPo.getpCode(), orderInfoPo.getGoodsId(), orderInfoPo.getServerId(), orderInfoPo.getMyOrderId(),
                    orderInfoPo.getPlatOrderId(), orderInfoPo.getGoodsPrice(), orderInfoPo.getPayaMount(), orderInfoPo.getActType(),
                    orderInfoPo.getOrderState(), orderInfoPo.getChannel(), orderInfoPo.getOrderCreateTime(), orderInfoPo.getUpDateTime(),
                    orderInfoPo.getCurs(), orderInfoPo.getAddVipExp(), orderInfoPo.getSaveToDbTime()};
            list.add(objects);
        }
        int[] ints = DbManager.instance().executeBatchCommand(REPLACE_ONE, list);
        return ints.length == orderInfoPoList.size();
    }

    public static void delete(String pCode, long updateTime) {
        int i = DbManager.instance().executeCommand(DELETE, new Object[]{pCode, updateTime});

    }


    public static OrderInfoPo findOne(String playerCode, String myorderid) {
        String sql = "select * from `orderInfo` where `pCode` = ? and `myOrderId` = ? ";
        List<OrderInfoPo> list = DbManager.instance().executeQueryObjectList(sql, new Object[]{playerCode, myorderid}, OrderInfoPo.builder);
        return (list != null && list.size() > 0) ? list.get(0) : null;
    }
}
